//
//  AddToListSheetView.m
//  FruitHelper
//
//  Created by 赵隆杰 on 2021/12/11.
//

#import "AddToListSheetView.h"

@interface AddToListSheetView ()<UITextFieldDelegate>

@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *textField;

@property (nonatomic, copy) NSString *fruitKg;

@end

@implementation AddToListSheetView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.layer.cornerRadius = 5;
        imageView.layer.masksToBounds = YES;
        imageView.backgroundColor = kLineColor;
        imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self.contentView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.width.height.equalTo(@100);
            make.top.equalTo(self.contentView);
        }];
        self.imageView = imageView;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:14];
        titleLabel.textColor = kColor666666;
        [self.contentView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.top.equalTo(imageView.mas_bottom).offset(10);
            make.height.equalTo(@40);
        }];
        self.titleLabel = titleLabel;
        
        UITextField *textField = [[UITextField alloc] init];
        textField.placeholder = @"请输入水果重量(kg)";
        textField.keyboardType = UIKeyboardTypeDecimalPad;
        textField.delegate = self;
        textField.borderStyle = UITextBorderStyleRoundedRect;
        [textField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
        [self.contentView addSubview:textField];
        [textField mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@50);
            make.centerX.equalTo(self);
            make.top.equalTo(titleLabel.mas_bottom).offset(20);
            make.height.equalTo(@40);
        }];
        self.textField = textField;
        
        UIButton *addButton = [[UIButton alloc] init];
        addButton.layer.cornerRadius = 25;
        addButton.layer.masksToBounds = YES;
        addButton.backgroundColor = kThemeColor;
        addButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [addButton setTitle:@"添加到列表" forState:UIControlStateNormal];
        [addButton addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:addButton];
        [addButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@50);
            make.height.equalTo(@50);
            make.centerX.equalTo(self);
            make.top.equalTo(textField.mas_bottom).offset(100);
        }];
        
        
    }
    return self;
}

- (void)setModel:(FruitModel *)model
{
    _model = model;
    if (model.imageType == 1) {
        UIImage *image = [NSObject Base64StrToUIImage:model.imageStr];
        self.imageView.image = image;
    } else {
        self.imageView.image = [UIImage imageNamed:model.imageStr];
    }
    self.titleLabel.text = model.title;
    self.textField.placeholder = [NSString stringWithFormat:@"请输入%@重量(kg)", model.title];
}

- (void)confirmBtnClick
{
    if (!self.fruitKg.length) {
        NSString *toastText = [NSString stringWithFormat:@"请输入%@重量", _model.title];
        [kAppWindow makeToast:toastText duration:2 position:CSToastPositionTop];
        return;
    }
    
    _model.kg = self.fruitKg;
    _model.addTime = [NSString getNowTimeTimestamp];
    [LocalData saveAddFruitInfoModel:_model];
    if (self.addToListBlock) {
        self.addToListBlock();
    }
    
    [self dismissView];
    [kAppWindow makeToast:@"已加入清单！" duration:2 position:CSToastPositionTop];
}

- (void)changedTextField:(UITextField *)textField {
    
    self.fruitKg = textField.text;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    [textField endEditing:YES];
    return YES;
}


@end
